# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2018/12/14
# python-version    : Python 3.7.0
# description       : 

import wx
import _thread as thread
from time import sleep

global conn


class ChatFrame(wx.Frame):
    "聊天窗口"

    def __init__(self, conn,parent, id, title, size):
        "初始化，添加控件并绑定事件"
        wx.Frame.__init__(self, parent, id, title)
        self.SetSize(size)
        self.Center()
        self.conn = conn
        self.chatFrame = wx.TextCtrl(self, pos=(5, 5), size=(490, 130), style=wx.TE_MULTILINE | wx.TE_READONLY)
        self.message = wx.TextCtrl(self, pos=(5, 320), size=(300, 25))
        self.sendButton = wx.Button(self, label="发送", pos=(310, 320), size=(58, 25))
        self.usersButton = wx.Button(self, label="所有用户", pos=(373, 320), size=(58, 25))
        self.closeButton = wx.Button(self, label="关闭", pos=(436, 320), size=(58, 25))
        # 发送按钮绑定发送消息方法
        self.sendButton.Bind(wx.EVT_BUTTON, self.send)
        # 所有用户按钮绑定获取在线用户数量方法
        self.usersButton.Bind(wx.EVT_BUTTON, self.lookUsers)
        # 关闭按钮绑定关闭方法
        self.closeButton.Bind(wx.EVT_BUTTON, self.close)
        thread.start_new_thread(self.receive, ())
        self.Show()

    def send(self, event):
        "发送消息"
        message = str(self.message.GetLineText(0)).strip()
        if message != '':
            self.conn.write(('说:' + message + '\n').encode('utf-8'))
            self.message.Clear()

    def lookUsers(self, event):
        "查看当前在线用户"
        self.conn.write(b'See All Users\n')

    def close(self, event):
        "关闭窗口"
        self.conn.write(b'Logout')
        self.conn.close()
        self.Close()

    def receive(self):
        "接收服务器消息"
        while True:
            sleep(0.6)
            result = self.conn.read_very_eager()
            if result != '':
                self.chatFrame.AppendText(result)
